package mysql

import (
	"video/videorpc/videorpc"

	"gorm.io/gorm"
)

type Collection struct {
	gorm.Model
	VideoId       int    `gorm:"type:int(11);not null"`
	UserId        int    `gorm:"type:int(11);not null"`
	Content       string `gorm:"type:varchar(500);not null"`
	CollectStatus int    `gorm:"tinyint:int(11);default:0"`
}

func (c *Collection) CommentAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error

}

func (c *Collection) FindComment(db *gorm.DB, UserId int64, VideoId int64) error {
	return db.Debug().Where("user_id=?", UserId).Where("video_id=?", VideoId).Find(c).Error
}

func (c *Collection) CommentDel(db *gorm.DB, UserId int64, VideoId int64) error {
	return db.Debug().Where("user_id=?", UserId).Where("video_id=?", VideoId).Delete(c).Error
}

func CollectionList(db *gorm.DB, in *videorpc.CollectionListRequest) []*videorpc.CollectionList {
	/*
			SELECT
		  videos.video_title,
		  videos.video_category,
		  videos.cover_link,
		  videos.play_num,
		  users.nick_name,
		  users.id
		FROM
		  collections
		  LEFT JOIN users ON collections.user_id = users.id
		  LEFT JOIN videos ON collections.video_id = videos.id
	*/
	var list []*videorpc.CollectionList
	db.Debug().Model(&Collection{}).
		Select(" videos.video_title,  videos.video_category,"+
			" videos.video_link,  videos.play_num,  users.nick_name,  users.id as user_id").
		Joins("LEFT JOIN users ON collections.user_id = users.id").
		Joins("LEFT JOIN videos ON collections.video_id = videos.id").
		Where("users.id=?", in.UserId).
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Order("collections.created_at desc").
		Find(&list)
	return list
}
